import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

# 设置图片的字体
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False


def bar_func(func):
    # some simple data
    x = [i for i in range(1, 6)]
    y = [6, 10, 4, 5, 1]
    # create bar
    labels = ['A', 'B', 'C', 'D', 'E']
    # tick_label 表示刻度标签值
    func(x, y, align='center', color='c', tick_label=labels, alpha=0.6)

    plt.xlabel('测试难度')
    plt.ylabel('试卷分数')

    # set yaxis grid
    plt.grid(axis='y', ls=':', color='r', alpha=0.3)
    plt.show()


def bar_bottom_func():
    x = [i for i in range(1, 6)]
    y = [6, 10, 4, 5, 1]
    y1 = [2, 6, 3, 8, 5]
    labels = ['A', 'B', 'C', 'D', 'E']

    # create bar
    plt.bar(x, y, align='center', color='#66c2a5', tick_label=labels, label='班级A')

    plt.bar(x, y1, align='center', bottom=y, color='#8da0cb', label='班级B')
    # set x,y_axis label
    plt.xlabel('测试难度')
    plt.ylabel('试卷分数')
    plt.legend()
    plt.show()


def multi_bar_func():
    # 多数据并列柱状图主要用来多数据的分布差距
    x = np.arange(5)
    y = [6, 10, 4, 5, 1]
    y1 = [2, 6, 3, 8, 5]

    bar_width = 0.35
    tick_label = ['A', 'B', 'C', 'D', 'E']

    # create bar
    plt.bar(x, y, bar_width, color='c', align='center', tick_label=tick_label,
            label='Class A', alpha=0.5)
    plt.bar(x + bar_width, y1, bar_width, color='b', align='center', label='Class B', alpha=0.5)

    # set x,y_axis label
    plt.xlabel('测试难度')
    plt.ylabel('试卷分数')
    plt.legend()
    plt.show()


def stackplot_func():
    x = np.arange(1, 6, 1)
    # x = np.linspace(1, 5, 5,dtype=int)
    y = [0, 4, 3, 5, 6]
    y1 = [1, 3, 4, 2, 7]
    y2 = [3, 4, 1, 6, 5]
    labels = ['BluePlanet', 'BrownPlanet', 'GreenPlanet']
    # colors = ['#8da0cb', '#fc8d62', '#66c2a5']
    colors = ['c', 'r', 'g']
    plt.stackplot(x, y, y1, y2, labels=labels, colors=colors)
    plt.legend(loc='upper left')
    plt.show()
    return


def broken_barh_func():
    plt.broken_barh([(30, 100), (180, 50), (260, 70)], (20, 8), facecolors='#1f78b4')
    plt.broken_barh([(60, 90), (190, 20), (230, 30), (280, 60)], (10, 8),
                    facecolors=('#7fc97f', '#beaed4', '#fdc086', '#ffff99'))
    plt.xlim(0, 360)
    plt.ylim(5, 35)
    plt.xlabel('演出时间')
    plt.xticks(np.arange(0, 361, 60))
    plt.yticks([15, 25], ['歌剧院A', '歌剧院B'])
    plt.grid(ls='-', lw=1, color='gray')
    plt.title('不同地区的歌剧院的演出时间对比')
    plt.show()
    return


def step_func(str):
    if str not in ('pre', 'post'):
        raise KeyError(str)

    x = np.linspace(1, 10, 10)
    y = np.sin(x)
    plt.step(x, y, color='#8dd3c7', where=str, lw=2)
    plt.axhline(y=0.0, c='r', ls='--', lw=2)  # h denotes horizontal
    plt.axvline(x=5.0, c='k', ls='-', lw=2)  # v denotes vertical
    plt.xlim(0, 11)
    plt.xticks(np.arange(1, 11, 1))
    plt.ylim(-1.2, 1.2)
    plt.show()


# TODO where = 'pre' or 'post'
def perceptron_activation_func():
    x = [-10, 0, 10]
    y = [-1, 0, 1]
    plt.step(x, y, color='c', where='pre', lw=2)
    # plt.axhline(y=0.0, c='r', ls='--', lw=2)  # h denotes horizontal
    # plt.axvline(x=0.0, c='k', ls='-', lw=2)  # v denotes vertical
    plt.xlim(-10, 15)
    plt.xticks(np.arange(-10, 15, 1))
    plt.ylim(-1, 1)
    plt.show()


def hist_func():
    """
        直方图 (histogram) 和柱状图 (bar) 的区别:
        1. 直方图描述连续型数据分布, 柱状图描述离散型数据的分布
        2. 柱状图柱体之间有空隙, 直方图的柱体之间没有空隙.
    """
    scoresT = np.random.randint(0, 100, 100)
    x = scoresT
    # plot histogram
    bins = range(0, 101, 10)
    plt.hist(x, bins=bins, color='#377e37', histtype='bar', rwidth=10)
    # set s,y-axis label
    plt.xlabel('测试成绩')
    plt.ylabel('学生人数')
    plt.show()


def stacking_hist_func(stacked=True, histtype='bar'):
    scoresT1 = np.random.randint(0, 100, 100)
    scoresT2 = np.random.randint(0, 100, 100)
    x = [scoresT1, scoresT2]
    colors = ['#8dd3ee', '#bebaee']
    labels = ['Class A', 'Class B']
    # plot stacking histogram
    bins = range(0, 101, 10)
    plt.hist(x, bins=bins,
             color=colors,
             histtype=histtype,
             rwidth=10,
             stacked=stacked,
             label=labels)
    # set x,y-axis label
    plt.xlabel('Testing Grade')
    plt.ylabel('Number of Students')
    plt.title('Histogram of test scores for different classes')
    plt.legend(loc='upper left')
    plt.show()


def pie_func():
    labels = 'A', 'B', 'C', 'D'
    students = [0.35, 0.15, 0.20, 0.30]
    colors = ["#377eb8", "#4daf4a", "#984ea3", "#ff7f00"]
    explode = (0.1, 0.1, 0.1, 0.1)
    # exploded pie chart
    plt.pie(students, explode=explode,
            labels=labels,
            autopct='%3.2f%%',
            startangle=45,
            shadow=True,
            colors=colors)
    plt.title('选择不同难度测试试卷的学生百分比')
    plt.show()


if __name__ == '__main__':
    # bar_func(plt.bar)
    # bar_func(plt.barh)
    # bar_bottom_func()
    # multi_bar_func()
    # print(np.arange(5))
    # stackplot_func()
    # broken_barh_func()
    # step_func('post')
    # perceptron_activation_func()
    # hist_func()
    stacking_hist_func(stacked=True, histtype='stepfilled')
    # pie_func()
